<template>
  <Header :title="{ zh: '开发制一目视管理看板', en: 'Sample Line-1 Visual Board', index: 11 }" />

  <div class="EchartsContainer">
    <div class="row">
      <div class="item" style="width: 40%">
        <div class="title">周产能状况<span style="color: red;">(双)</span></div>
        <div class="echarts" id="main"></div>
      </div>
      <div class="item" style="width: 40%">
        <div class="title">月产能状况<span style="color: red;">(双)</span></div>
        <div class="echarts" id="main2"></div>
      </div>
      <div class="item" style="width: 40%">
        <div class="title">面底配套状况<span style="color: red;">(双)</span></div>
        <div class="echarts" id="main3"></div>
      </div>

    </div>

    <div class="row" style="margin-top: 10px">
      <swiper :autoplay="{
        delay: 60000
      }" :slides-per-view="1" :space-between="2000" style="height: 100%; width: 100%">
        <swiper-slide>
          <div class="item">
            <div class="title" style="text-align: start">针车日计划</div>
            <el-table ref="tab" border class="table" :data="tableData" style="width: 100%; height: 100%">
              <el-table-column align="center" prop="brand" label="品牌" />
              <el-table-column align="center" prop="lotNo" label="指令号" />
              <el-table-column align="center" prop="photoPath" label="鞋图 ">
                <template v-slot="{ row, $index }">
                  <el-image hide-on-click-modal preview-teleported style="height: 34px" :src="row.photoPath"
                    :zoom-rate="1.2" @click="imgClicked(row.photoPath, srcList)" :preview-src-list="srcList"
                    :initial-index="imgIndex" fit="cover" />
                </template>
              </el-table-column>
              <el-table-column align="center" prop="orderQty" label="订单数量" />
              <el-table-column align="center" prop="planQty" label="计划数量" />
              <el-table-column align="center" prop="finQty" label="完成数量" />
              <el-table-column align="center" prop="planDate" label="计划完成日" />
            </el-table>
          </div>
        </swiper-slide>
        <swiper-slide>
          <div class="item">
            <div class="title" style="text-align: start">成型日计划</div>
            <el-table ref="tab2" border class="table" :data="tableData2" style="width: 100%; height: 100%">
              <el-table-column align="center" prop="brand" label="品牌" />
              <el-table-column align="center" prop="lotNo" label="指令号" />
              <el-table-column align="center" prop="photoPath" label="鞋图 ">
                <template v-slot="{ row, $index }">
                  <el-image hide-on-click-modal preview-teleported style="height: 34px" :src="row.photoPath"
                    :zoom-rate="1.2" @click="imgClicked(row.photoPath, srcList)" :preview-src-list="srcList"
                    :initial-index="imgIndex" fit="cover" />
                </template>
              </el-table-column>
              <el-table-column align="center" prop="orderQty" label="订单数量" />
              <el-table-column align="center" prop="planQty" label="计划数量" />
              <el-table-column align="center" prop="finQty" label="完成数量" />
              <el-table-column align="center" prop="planDate" label="计划完成日" />
            </el-table>
          </div>
        </swiper-slide>
        <swiper-slide>
          <div class="item">
            <div class="title" style="text-align: start">面底配套明细</div>
            <el-table ref="tab3" border class="table" :data="tableData3" style="width: 100%; height: 100%">
              <el-table-column align="center" prop="brand" label="品牌" />
              <el-table-column align="center" prop="lotNo" label="指令号" />
              <el-table-column align="center" prop="photoPath" label="鞋图 ">
                <template v-slot="{ row, $index }">
                  <el-image hide-on-click-modal preview-teleported style="height: 34px" :src="row.photoPath"
                    :zoom-rate="1.2" @click="imgClicked(row.photoPath, srcList)" :preview-src-list="srcList"
                    :initial-index="imgIndex" fit="cover" />
                </template>
              </el-table-column>
              <el-table-column align="center" prop="orderQty" label="订单数量" />
              <el-table-column align="center" prop="unFinQty" label="未完成数" />
              <el-table-column align="center" prop="finQty" label="配套库存数" />
            </el-table>
          </div>
        </swiper-slide>
      </swiper>

    </div>
  </div>
</template>

<script setup lang="ts">
import { nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
import Header from '../components/Header.vue'
import * as echarts from 'echarts'
import { ElMessage } from 'element-plus'
import request from '@/utils/request'
import { Swiper, SwiperSlide } from 'swiper/vue'

const srcList = ref([]);
const imgIndex = ref(0);
// 点击图片
const imgClicked = (src: any, srcList: any) => {
  let index = srcList.findIndex((item: any) => item === src);
  imgIndex.value = index;
};


let rollTimer = ref() as any
const tab = ref<HTMLElement>() as any
const tab2 = ref<HTMLElement>() as any
const tab3 = ref<HTMLElement>() as any
const autoRoll = (stop: any) => {
  if (stop) {
    clearInterval(rollTimer.value)
    return
  }
  // 这里的 tab 是上方 table 表格绑定的ref值
  const divData = tab.value.layout.table.refs.bodyWrapper.firstElementChild.firstElementChild
  const divData2 = tab2.value.layout.table.refs.bodyWrapper.firstElementChild.firstElementChild
  const divData3 = tab3.value.layout.table.refs.bodyWrapper.firstElementChild.firstElementChild
  rollTimer.value = setInterval(() => {
    // + 4 是每秒向下滑动 4个像素  这块可以自己更改
    divData.scrollTop += 1
    divData2.scrollTop += 1
    // console.log(divData.scrollTop)
    // 下方判断是滑动到底部就会自己回到最上方重新开始滑动  改动像素的话 一定要满足进入这个判断  否则滚动到底部就停了
    if (divData.clientHeight + divData.scrollTop == divData.scrollHeight) {
      divData.scrollTop = 0
    }
    if (divData2.clientHeight + divData2.scrollTop == divData2.scrollHeight) {
      divData2.scrollTop = 0
    }
    if (divData3.clientHeight + divData3.scrollTop == divData3.scrollHeight) {
      divData3.scrollTop = 0
    }
  }, 150)
}
onMounted(() => {
  autoRoll(false)
})
onBeforeUnmount(() => {
  autoRoll(true)
})


const showData = ref({
  chart1: {
    data: [
      {
        name: '针车组',
        type: 'bar',
        barGap: 0,
        label: {
          show: true,
          position: 'top'
        },
        emphasis: {
          focus: 'series'
        },
        data: [5322, 11536, 10321, 3550, 0, 0]
      },
      {
        name: '成型组',
        type: 'bar',
        barGap: 0,
        label: {
          show: true,
          position: 'top'
        },
        emphasis: {
          focus: 'series'
        },
        data: [4019, 7052, 6749, 2370, 0, 0]
      },
    ]
  },
  chart2: {
    data: []
  },
  chart3: {
    data: []
  },

}) as any
// 获取针车
const DeptNos = ref([]) as any
const DeptName = ref([]) as any
const getDeptTypes = async () => {
}
// 获取成型
const DeptNos2 = ref([]) as any
const DeptName2 = ref([]) as any
const getDeptTypes2 = async () => {
}

const getData = async () => {

  // try {
  //   // 获取表格数据
  //   let tableDataList = (await request.post<any, any>(`/procPlan/show/plan?deptType=${'针车'}&type=${'日'}`, {
  //     pageNum: '1',
  //     pageSize: '200'
  //   })) as any
  //   tableData.value = tableDataList.dto
  //   let tableDataList2 = (await request.post<any, any>(`/procPlan/show/plan?deptType=${'成型'}&type=${'日'}`, {
  //     pageNum: '1',
  //     pageSize: '200'
  //   })) as any
  //   tableData2.value = tableDataList2.dto
  //   let tableDataList3 = (await request.post<any, any>(`/lotMgt/match/show?deptType=${'成型'}&type=${'日'}`, {
  //     pageNum: '1',
  //     pageSize: '200'
  //   })) as any
  //   tableData3.value = tableDataList3.dto
  // } catch (error: any) {
  //   ElMessage.error(error)
  // }
}
// 表格数据
const tableData = ref([])
const tableData2 = ref([])
const tableData3 = ref([])
onMounted(async () => {
  await getDeptTypes()
  await getDeptTypes2()
  await getData()
  getEchartsDom()
})
const getEchartsDom = () => {
  let chartDom = document.getElementById('main')
  let myChart = echarts.init(chartDom)
  let app = {} as any
  const posList = [
    'left',
    'right',
    'top',
    'bottom',
    'inside',
    'insideTop',
    'insideLeft',
    'insideRight',
    'insideBottom',
    'insideTopLeft',
    'insideTopRight',
    'insideBottomLeft',
    'insideBottomRight'
  ]
  app.configParameters = {
    rotate: {
      min: -90,
      max: 90
    },
    align: {
      options: {
        left: 'left',
        center: 'center',
        right: 'right'
      }
    },
    verticalAlign: {
      options: {
        top: 'top',
        middle: 'middle',
        bottom: 'bottom'
      }
    },
    position: {
      options: posList.reduce(function (map: any, pos) {
        map[pos] = pos
        return map
      }, {})
    },
    distance: {
      min: 0,
      max: 100
    }
  }
  app.config = {
    rotate: 90,
    align: 'left',
    verticalAlign: 'middle',
    position: 'insideBottom',
    distance: 15,
    onChange: function () {
      const labelOption = {
        rotate: app.config.rotate,
        align: app.config.align,
        verticalAlign: app.config.verticalAlign,
        position: app.config.position,
        distance: app.config.distance
      }
      myChart.setOption({
        series: [
          {
            label: labelOption
          },
          {
            label: labelOption
          },
          {
            label: labelOption
          },
          {
            label: labelOption
          }
        ]
      })
    }
  }
  let option = {
    color: ['#72DDB3', '#93BEFF'],
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: 60,
      right: 0,
      top: 30,
      bottom: 45
    },
    legend: {
      data: ['针车组', '成型组'],
      top: 'bottom'
    },
    xAxis: [
      {
        type: 'category',
        axisTick: { show: false },
        data: ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
      }
    ],
    yAxis: [
      {
        type: 'value'
      }
    ],
    series: showData.value.chart1.data
  }
  option && myChart.setOption(option)


  // 第二张图
  let chartDom2 = document.getElementById('main2')
  let myChart2 = echarts.init(chartDom2)
  let option2 = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        crossStyle: {
          color: '#999'
        }
      }
    },
    grid: {
      left: 60,
      right: 0,
      top: 30,
      bottom: 45
    },
    xAxis: [
      {
        type: 'category',
        data: ['针车', '成型'],
        axisPointer: {
          type: 'shadow'
        }
      }
    ],
    yAxis: [
      {
        type: 'value',
        min: 0,
        // max: 1000,
        // interval: 200,
        axisLabel: {
          formatter: '{value}'
        }
      }
    ],
    series: [
      {
        type: 'bar',
        label: {
          show: true,
          position: 'top'
        },
        barWidth: '15%',
        tooltip: {
          valueFormatter: function (value: any) {
            return value
          }
        },
        itemStyle: {
          color: new echarts.graphic.LinearGradient(
            0,
            0,
            0,
            1, // 渐变方向：从上到下
            [
              { offset: 0, color: '#3662EC' },
              { offset: 1, color: 'white' }
            ]
          )
        },
        data: [30729, 20190]
        // data: showData.value.chart2.data
      }
    ]
  }
  option2 && myChart2.setOption(option2)

  // 第三张图
  let chartDom3 = document.getElementById('main3')
  let myChart3 = echarts.init(chartDom3)
  let option3 = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        crossStyle: {
          color: '#999'
        }
      }
    },
    grid: {
      left: 60,
      right: 0,
      top: 30,
      bottom: 45
    },
    xAxis: [
      {
        type: 'category',
        axisTick: { show: false },
        data: ['鞋面库存', '大底库存', '面底配套']
      }
    ],
    yAxis: [
      {
        type: 'value'
      }
    ],
    series: {
      type: 'bar',
      label: {
        show: true,
        position: 'top'
      },
      barWidth: '15%',
      tooltip: {
        valueFormatter: function (value: any) {
          return value
        }
      },
      itemStyle: {
        color: new echarts.graphic.LinearGradient(
          0,
          0,
          0,
          1, // 渐变方向：从上到下
          [
            { offset: 0, color: '#FA7358' },
            { offset: 1, color: 'white' }
          ]
        )
      },
      data: [141716, 141735, 105027]
      // data: showData.value.chart3.data
    }
  }
  option3 && myChart3.setOption(option3)
}


onMounted(() => {
  function One() {
    if (location.href.indexOf('#reloaded') == -1) {
      location.href = location.href + '#reloaded'
      location.reload()
    }
  }
  One()
})
</script>

<style scoped lang="less">
.EchartsContainer {
  border-radius: 4px;
  margin-top: 10px;
  height: calc(100vh - 7vh - 40px);
  width: 100%;
}

.row {
  display: flex;
  height: 50%;
  width: 100%;

  .item {
    background-color: white;
    width: 100%;
    display: flex;
    flex-direction: column;
    height: 100%;
    padding-bottom: 10px;
    padding: 0 5px 5px;

    // align-items: center;
    .title {
      font-weight: 900;
      font-size: 1rem;
      margin-top: 10px;
      text-align: center;
      color: black;
    }

    .echarts {
      flex: 1;
    }
  }
}

:deep(thead) {
  .is-center {
    color: white;
    background-color: #ff9940;
  }
}
</style>
